package net.angrynut.web.configuration;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

/**
 * 这里用来集中处理异常之类的。
 */
@ControllerAdvice
public class ExceptionHandlers extends ResponseEntityExceptionHandler {

	/**
	 * 使用 <tt>@ExceptionHandler</tt> 处理异常。
	 */
	@ExceptionHandler(RuntimeException.class)
	@ResponseBody
	ResponseEntity<Map<String, Object>> handleControllerException(HttpServletRequest request, Throwable ex) {
		Map<String, Object> map = new HashMap<>();
		map.put("errorMsg", ex.getMessage());
		map.put("exception", RuntimeException.class.getName());
		return new ResponseEntity<>(map, HttpStatus.BAD_REQUEST);
	}
}
